package com.hospital.common.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Collections;

/**
 * @author lvyao
 * @description redis实现类
 * @date 2020/5/13
 */
@Slf4j
public class RedisUtils {
    private static final String LOCK_SUCCESS = "OK";
    private static final String SET_IF_NOT_EXIST = "NX";
    private static final String SET_WITH_EXPIRE_TIME = "PX";
    private static final String SET_EXPIRED="EX";

    private volatile static JedisPool jedisPool=SpringContextHolder.getBean(JedisPool.class);


    /**
     * 尝试获取分布式锁
     * @param lockKey 锁
     * @param requestId 请求标识
     * @param expireTime 超期时间
     * @return 是否获取成功
     */
    public static boolean tryGetDistributedLock( String lockKey, String requestId, int expireTime) {
        Jedis jedis=null;
        try{
            jedis=jedisPool.getResource();
            jedis.select(0);

            String result = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);

            if (LOCK_SUCCESS.equals(result)) {
                return true;
            }
        }catch (Exception ex){
            log.error("报错信息",ex);
        }finally {
            if(jedis!=null) {
                jedis.close();
            }
        }
        return false;
    }
    private static final Long RELEASE_SUCCESS = 1L;

    /**
     * 释放分布式锁
     * @param lockKey 锁
     * @param requestId 请求标识
     * @return 是否释放成功
     */
    public static boolean releaseDistributedLock( String lockKey, String requestId) {
        Jedis jedis=null;
        try{
            jedis=jedisPool.getResource();
            jedis.select(0);

            String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
            Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));

            if (RELEASE_SUCCESS.equals(result)) {
                return true;
            }

        }catch (Exception ex){
            log.error("报错信息",ex);
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        return false;

    }
    /**
     * 获取key值
     * @param keyName
     * @return
     */
    public static String getKey(String keyName){
        Jedis jedis = null;
        try{
            jedis=jedisPool.getResource();
            jedis.select(0);

            String result = jedis.get(keyName);

            if (result!=null&&!result.equals("")) {
                return result;
            }
        }catch (Exception ex){
            log.error("报错信息",ex);
        }finally {
            if(jedis!=null) {
                jedis.close();
            }
        }
        return null;
    }

    public static boolean setValEx(String key,String value,long expiredTime){
        Jedis jedis=null;
        String resVal=null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(0);
            resVal= jedis.set(key, value,SET_IF_NOT_EXIST,SET_EXPIRED,expiredTime);
        }catch (Exception e){
            log.error("插入数据",e);
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        return resVal!=null;
    }

    /**
     * 设置key值
     * @param keyName
     * @param keyValue
     * @return
     */
    public static String setKey(String keyName,String keyValue){
        Jedis jedis = null;
        try{
            jedis=jedisPool.getResource();
            jedis.select(0);

            String result = jedis.set(keyName,keyValue);

            if (result!=null&&!result.equals("")) {
                return result;
            }
        }catch (Exception ex){
            log.error("报错信息",ex);
        }finally {
            if(jedis!=null) {
                jedis.close();
            }
        }
        return null;
    }

    /**
     * 检查key是否存在
     * @param keyName
     * @return
     */
    public static boolean isExistsKey(String keyName){
        Jedis jedis = null;
        try{
            jedis=jedisPool.getResource();
            jedis.select(0);

            Boolean exists = jedis.exists(keyName);

            if (exists) {
                return true;
            }
        }catch (Exception ex){
            log.error("报错信息",ex);
        }finally {
            if(jedis!=null) {
                jedis.close();
            }
        }
        return false;
    }

    /*** 设置key有效时间

     *@paramkey

     *@paramseconds*/

    public static void expire(String key, int seconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            Long expire = jedis.expire(key, seconds);

        } catch (Exception ex) {
            log.error("报错信息", ex);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

}
